package com.next.aappublicapp.tasks;

import java.io.InputStream;
import java.net.URL;

import com.next.aappublicapp.R;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
	ImageView bmImage;
	ProgressBar progressBar;

	public DownloadImageTask(ImageView bmImage) {
		this(bmImage,null);
	}

	public DownloadImageTask(ImageView bmImage,ProgressBar progressBar ) {
		Log.i("DownloadImageTask", "Download task created");
	    this.bmImage = bmImage;
	    this.progressBar = progressBar;
	}
	protected void onPreExecute() {
		if(progressBar != null){
			progressBar.setVisibility(View.VISIBLE);
			bmImage.setVisibility(View.GONE);
		}
	}
	protected Bitmap doInBackground(String... urls) {
	    String urldisplay = urls[0];
	    Log.i("DownloadImageTask", "Running Download Task for url "+ urldisplay);
	    Bitmap mIcon11 = null;
	    try {
	    	BitmapFactory.Options options = new BitmapFactory.Options();
	    	options.inJustDecodeBounds = true;
	    	URL imageUrl = new URL(urldisplay);
	        InputStream in = imageUrl.openStream();
	        mIcon11 = BitmapFactory.decodeStream(in,null,options);
	        Log.i("Ravi", "Image width : "+options.outWidth+" , "+ bmImage.getWidth());
	        Log.i("Ravi", "Image Height : "+options.outHeight+" , "+ bmImage.getHeight());
	        
	        int inSampleSize = 1;
	        if (/*options.outHeight > bmImage.getHeight() ||*/ options.outWidth > bmImage.getWidth()) {

	            // Calculate ratios of height and width to requested height and width
	            //final int heightRatio = Math.round((float) options.outHeight / (float) bmImage.getHeight());
	            final int widthRatio = Math.round((float) options.outWidth / (float) bmImage.getWidth());

	            // Choose the smallest ratio as inSampleSize value, this will guarantee
	            // a final image with both dimensions larger than or equal to the
	            // requested height and width.
	            inSampleSize = widthRatio;//heightRatio < widthRatio ? heightRatio : widthRatio;
	            bmImage.getLayoutParams().height = options.outHeight / inSampleSize;
	        }
	        Log.i("Ravi", "inSampleSize : "+inSampleSize);
	        options.inSampleSize = inSampleSize;
	        options.inJustDecodeBounds = false;
	        in = imageUrl.openStream();
	        mIcon11 = BitmapFactory.decodeStream(in,null,options);
	    } catch (Throwable e) {
	        e.printStackTrace();
	    }
	    Log.i("DownloadImageTask", "Download Task Finished");
	    //return roundCorner(mIcon11,10);
	    return mIcon11;
	}
	public static Bitmap roundCorner(Bitmap src, float round) 
	{
	    // image size
	    int width = src.getWidth();
	    int height = src.getHeight();

	    // create bitmap output
	    Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);

	    // set canvas for painting
	    Canvas canvas = new Canvas(result);
	    canvas.drawARGB(0, 0, 0, 0);

	    // config paint
	    final Paint paint = new Paint();
	    paint.setAntiAlias(true);
	    paint.setColor(Color.WHITE);

	    // config rectangle for embedding
	    final Rect rect = new Rect(0, 0, width, height);
	    final RectF rectF = new RectF(rect);

	    // draw rect to canvas
	    canvas.drawRoundRect(rectF, round, round, paint);

	    // create Xfer mode
	    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	    // draw source image to canvas
	    canvas.drawBitmap(src, rect, rect, paint);

	    // return final image
	    return result;
	}

	protected void onPostExecute(Bitmap result) {
		try{
			 Log.i("DownloadImageTask", "Image Loaded");
				if(progressBar != null){
					progressBar.setVisibility(View.GONE);
					bmImage.setVisibility(View.VISIBLE);
				}
				if(result != null){
					bmImage.setImageBitmap(result);	
				}
		}catch(Exception ex){
			
		}
	}
}